package com.ruoyi.quartz.race.tool;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.quartz.race.model.RaceEntity;
import com.ruoyi.quartz.race.model.RaceTouzhu;
import com.ruoyi.quartz.util.JedisUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 分析类
 */
public class TouzhuUtil implements PageProcessor {
	private Site site = Site.me().setDomain("500.com").setSleepTime(10000)
			.setUserAgent(
					"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31")
			.addHeader("referer", "https://odds.500.com").addHeader("sec-fetch-dest", "empty")
			.addHeader("sec-fetch-mode", "cors").addHeader("sec-fetch-site", "same-origin")
			.addHeader("x-requested-with", "XMLHttpRequest");
	private RaceEntity raceEntity;

	public TouzhuUtil(RaceEntity ce) {
		this.raceEntity = ce;
	}

	@Override
	public void process(Page page) {
		String pagetext = page.getRawText();
		Selectable pds = new Html(pagetext).$(".M_box");
		List<Selectable> trs = pds.nodes();
		// 热度
		Selectable hotHtml = trs.get(0);
		List<Selectable> hotzss = hotHtml.$("tr").nodes();
		RaceTouzhu racetz = new RaceTouzhu();
		racetz.setId(this.raceEntity.getId());
		// 主队
		List<Selectable> wds = hotzss.get(2).$("td").nodes();
		String wpl = wds.get(1).$("td", "text").get();
		String wbl = wds.get(2).$("td", "text").get();
		String wbf = wds.get(4).$("td", "text").get();

		// 平
		List<Selectable> dds = hotzss.get(3).$("td").nodes();
		String dpl = dds.get(1).$("td", "text").get();
		String dbl = dds.get(2).$("td", "text").get();
		String dbf = dds.get(4).$("td", "text").get();

		// 付
		List<Selectable> lds = hotzss.get(4).$("td").nodes();
		String lpl = lds.get(1).$("td", "text").get();
		String lbl = lds.get(2).$("td", "text").get();
		String lbf = lds.get(4).$("td", "text").get();

		racetz.setWin(wpl);
		racetz.setDraw(dpl);
		racetz.setLose(lpl);

		racetz.setPwin(wbl);
		racetz.setPdraw(dbl);
		racetz.setPlose(lbl);

		racetz.setBfwin(wbf);
		racetz.setBfdraw(dbf);
		racetz.setBflose(lbf);

		racetz.setBigwin("");
		racetz.setBigdraw("");
		racetz.setBiglose("");

		// 必发大额成交量
		Selectable bfs = new Html(pagetext).$(".data-detail");
		List<Selectable> bfnodes = bfs.nodes();
		Selectable bfbigHtml = bfnodes.get(1);
		List<Selectable> bfbigzss = bfbigHtml.$("li").nodes();
		if (bfbigzss.size() == 3) {
			String bwin = bfbigzss.get(0).$("li", "text").get();
			String bdraw = bfbigzss.get(1).$("li", "text").get();
			String blose = bfbigzss.get(2).$("li", "text").get();
			racetz.setBigwin(getPercent(bwin, bdraw, blose, 1));
			racetz.setBigdraw(getPercent(bwin, bdraw, blose, 2));
			racetz.setBiglose(getPercent(bwin, bdraw, blose, 3));
		}

		String gm = hotzss.get(5).$("td").nodes().get(1).$("td", "text").get();
		if (gm.contains("较小")) {
			racetz.setGm("规模较小");
		} else if (gm.contains("适中")) {
			racetz.setGm("规模适中");
		} else {
			racetz.setGm("规模较大");
		}
		List<Selectable> qxs = hotzss.get(6).$("td").nodes().get(0).$(".ying").nodes();
		String tj = qxs.get(0).$("em", "text").get();
		String xc = qxs.get(1).$("em", "text").get();
		racetz.setQx("推荐-" + tj + "-差异-" + xc);

		
		
		String key = "touzhu:" + racetz.getId();
		JedisUtil.set(key, JSON.toJSONString(racetz));
	}

	private String getPercent(String win, String draw, String lose, int ind) {
		win = win.replace(",", "");
		draw = draw.replace(",", "");
		lose = lose.replace(",", "");
		int total = Integer.parseInt(win) + Integer.parseInt(draw) + Integer.parseInt(lose);

		float num = 0f;
		if (ind == 1) {
			num = Float.parseFloat(win);
		} else if (ind == 2) {
			num = Float.parseFloat(draw);
		} else {
			num = Float.parseFloat(lose);
		}
		// 创建一个数值格式化对象
		java.text.NumberFormat numberformat = java.text.NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberformat.setMaximumFractionDigits(1);
		String result1 = numberformat.format(num / (float) total * 100);
		return result1+"%";
	}

	@Override
	public Site getSite() {
		return site;
	}

	public static void main(String[] args) {
		RaceEntity ce = new RaceEntity();
		ce.setId("1100083");
		new TouzhuUtil(ce).start();
	}

	public void start() {
		Spider.create(this).addUrl("https://odds.500.com/fenxi/touzhu-" + this.raceEntity.getId() + ".shtml").run();
	}
}
